<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul id="myul">

    </ul>
    <ul id="myul2">

    </ul>
    <ul id="myul3">

    </ul>
    <input type="button" value="ajax1" onclick="sendAjax1()">
    <input type="button" value="ajax2" onclick="sendAjax2()">
    <input type="button" value="ajax3" onclick="sendAjax3()">
    <input type="button" value="ajax4" onclick="sendAjax4()">

    <script>
        function sendAjax1() {
            let xhr = new XMLHttpRequest();
            let myul = document.getElementById("myul");
            xhr.open("get", "/params/text");
            xhr.onload = function () {
                let str = xhr.responseText;
                let arrs = str.split(",");
                let strs = "";
                arrs.forEach(function (item, index) {
                    strs += `<li>${item}</li>`;
                });
                myul.innerHTML = strs;
            };
            xhr.send();
        }

        function sendAjax2() {
            let xhr = new XMLHttpRequest();
            let myul2 = document.getElementById("myul2");
            xhr.open("get", "/params/html");
            xhr.onload = function () {
                let str = xhr.responseText;
                myul2.innerHTML = str;
            };
            xhr.send();
        }
        function sendAjax3() {
            let xhr = new XMLHttpRequest();
            let myul3 = document.getElementById("myul3");
            xhr.open("get", "/params/arr");
            xhr.onload = function () {
                let arrs = JSON.parse(xhr.responseText);
                let strs = "";
                arrs.forEach(function (item, index) {
                    strs += `<li>${item}</li>`;
                });
                myul3.innerHTML = strs;
            };
            xhr.send();
        }
        function sendAjax4() {
            let xhr = new XMLHttpRequest();
            let myul3 = document.getElementById("myul3");

            // xhr.responseType手动设置返回数据的类型，如果取值为空字符串，默认为"text"类型
            // 取值为json，表示ajax在请求成功且数据接收完成后，返回的json格式的字符串会自动调用JSON.parse解析操作
            // 一旦设置类型为非text类型，转换后的数据必须通过xhr.response获取解析后的数据
            // 此时xhr.responseText的取值程序会报错
            xhr.responseType = "json";
            xhr.open("get", "/params/arr");
            xhr.onload = function () {
                let arrs = xhr.response;
                let strs = "";
                arrs.forEach(function (item, index) {
                    strs += `<li>${item}</li>`;
                });
                myul3.innerHTML = strs;
            };
            xhr.send();
        }
    </script>
</body>

</html>